package com.jy.wms.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.dao.*;
import com.jy.wms.pojo.*;
import net.sf.json.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2018/1/31.
 */
@Service
public class WmsReportsService extends BaseService<WmsReportsMapper, WmsHarvestDaily> {
    @Resource
    private WmsStockMapper wmsStockMapper;
    @Resource
    private WmsShipmentOrderMapper wmsShipmentOrderMapper;
    @Resource
    private WmsLibraryPositionMapper wmsLibraryPositionMapper;
    @Resource
    private WmsPickingMapper wmsPickingMapper;

    /**
     * 收货日报表分页查询
     *
     * @param entity 实体
     * @return Page<WmsLine>
     */
    @BaseServiceDescribe(moduleName = "收货日报表", btnName = "分页查询")
    public Page<WmsHarvestDaily> baseHarvestPage(WmsHarvestDaily entity) throws Exception {
        Page<WmsHarvestDaily> list = null;
        try {
            PageHelper.startPage(entity.getPage(), entity.getRows());
            return (Page<WmsHarvestDaily>) dao.baseHarvestPage(entity);
        } catch (Exception e) {
            throw new ServiceException("收货日报表分页查询失败", e);
        }
    }

    /**
     * 收货日报表导出
     * @param ids
     * @param map
     * @return
     */
    @BaseServiceDescribe(moduleName = "收货日报表导出", btnName = "收货日报表导出")
    public List<WmsHarvestDaily> getOutPutHarvest(List<String> ids, Map map) {
        List<WmsHarvestDaily> list = dao.getOutPutHarvest(ids, map);
        return list;

    }

    /**
     * 上架日报表分页查询
     *
     * @param entity 实体
     * @return Page<WmsLine>
     */
    @BaseServiceDescribe(moduleName = "上架日报表", btnName = "分页查询")
    public Page<WmsShelvesDailies> baseWmsShelvesDailiesPage(WmsShelvesDailies entity) throws Exception {
        Page<WmsShelvesDailies> list = null;
        try {
            PageHelper.startPage(entity.getPage(), entity.getRows());
            list = (Page<WmsShelvesDailies>) dao.baseWmsShelvesDailiesPage(entity);
            return list;
        } catch (Exception e) {
            throw new ServiceException("上架日报表分页查询失败", e);
        }
    }

    /**
     * 上架日报表导出
     * @param ids
     * @param map
     * @return
     */
    @BaseServiceDescribe(moduleName = "收货日报表导出", btnName = "收货日报表导出")
    public List<WmsShelvesDailies> getOutPutShelves(List<String> ids, Map map) {
        return dao.getOutPutShelves(ids, map);
    }
    /**
     * JingKuc
     * @return
     */
    @BaseServiceDescribe(moduleName = "当前货品库存总数量", btnName = "当前货品库存总数量")
    public void Jingkc() throws Exception{
        try {
            WmsOutStock wmsOutStock = new WmsOutStock();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Date date = new Date();//获取当前年月
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH) + 1;
            wmsOutStock.setNy(sdf.format(date));
            wmsOutStock.setN(String.valueOf(year));
            wmsOutStock.setY(String.valueOf(month));
            List<WmsOutStock> outStocks = dao.outStock(wmsOutStock);
            if (outStocks.size() > 0) {
                WmsGoodsBalance wgb = null;
                List<WmsGoodsBalance> lists = new ArrayList<WmsGoodsBalance>();
                for (int i = 0; i < outStocks.size(); i++) {
                    SimpleDateFormat format = new SimpleDateFormat("M");
                    Calendar cals = Calendar.getInstance();
                    cals.add(Calendar.MONTH, -1);//查询上一个月
                    Date m = cals.getTime();
                    String mon = format.format(m);//上个月
                    int syear = cals.get(Calendar.YEAR);
                    //查询上月库存
                    wgb = new WmsGoodsBalance();
                    wgb.setId(UUIDUtils.uuid());
                    wgb.setZjrk(outStocks.get(i).getZjrk());
                    wgb.setZjck(outStocks.get(i).getZjck());
                    wgb.setHpid(outStocks.get(i).getHpid());//货品id
                    wgb.setHpbh(outStocks.get(i).getHpbh());//货品编号
                    wgb.setHpmc(outStocks.get(i).getHpmc());//货品名
                    wgb.setN(String.valueOf(year));//年
                    wgb.setY(String.valueOf(month));//月
                    wgb.setSj(sdf.format(date));
                    wgb.setShsl(outStocks.get(i).getShsl());
                    wgb.setCkid(outStocks.get(i).getCkid());
                    wgb.setHzid(outStocks.get(i).getHzid());
                    int ck=Integer.parseInt(outStocks.get(i).getCksl())+Integer.parseInt(outStocks.get(i).getKsck());
                    wgb.setCksl(String.valueOf(ck));
                    wgb.setHpslck(outStocks.get(i).getHpslck());
                    wgb.setHpslrk(outStocks.get(i).getHpslrk());
                    /*if (wgbs!=null){
                        wgb.setSyjc(wgbs.getByjc());//上月库存
                    }else {
                        wgb.setSyjc("0");//上月库存
                    }
                    wgb.setByjc(list.get(i).getKczsl());//本月库存*/
                    System.out.println(outStocks.get(i).getHpid());
                    Integer f=dao.seachHpkc(outStocks.get(i).getHpid());//获取目前为止的库存数
                    WmsGoodsBalance wgb1 = dao.byjcselect(String.valueOf(syear), mon, outStocks.get(i).getHpid());
                    if (wgb1 != null) {
                        wgb.setSyjc(wgb1.getByjc());
                        //本月结存=上月结存+收货数-出库质检+入库质检-出库数-快速出库数-不合格出库
                        int bysl = Integer.parseInt(wgb1.getByjc())+Integer.parseInt(outStocks.get(i).getShsl()) - Integer.parseInt(outStocks.get(i).getZjck()) + Integer.parseInt(outStocks.get(i).getZjrk()) - Integer.parseInt(outStocks.get(i).getCksl()) - Integer.parseInt(outStocks.get(i).getHpslck()) - Integer.parseInt(outStocks.get(i).getKsck());
                        wgb.setByjc(String.valueOf(bysl));
                    } else {
                        //无上月结存=现有库存数
                        wgb.setSyjc("0");
                        wgb.setByjc(String.valueOf(f));
                    }
//                    wgb.setSyjc("0");
//                    int bysl=f-Integer.parseInt(outStocks.get(i).getShsl())+Integer.parseInt(outStocks.get(i).getZjck())-Integer.parseInt(outStocks.get(i).getZjrk())+Integer.parseInt(outStocks.get(i).getCksl())+Integer.parseInt(outStocks.get(i).getHpslck())+Integer.parseInt(outStocks.get(i).getKsck());
//                    wgb.setByjc(String.valueOf(bysl));
                    if (wgb.getHpbh() != null) {
                        lists.add(wgb);
                    }
                }
                //添加结存数据
                dao.baseInsertShelves(lists);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 货品库存总数量
     * @return
     */
    @BaseServiceDescribe(moduleName = "货品库存总数量", btnName = "货品库存总数量")
    public void quantity() throws Exception {
        try {
            WmsOutStock wmsOutStock = new WmsOutStock();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Date date = new Date();//获取当前年月
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH) + 1;
            wmsOutStock.setNy(sdf.format(date));
            wmsOutStock.setN(String.valueOf(year));
            wmsOutStock.setY(String.valueOf(month));
            List<WmsOutStock> outStocks = dao.outStock(wmsOutStock);
            if (outStocks.size() > 0) {
                WmsGoodsBalance wgb = null;
                List<WmsGoodsBalance> lists = new ArrayList<WmsGoodsBalance>();
                for (int i = 0; i < outStocks.size(); i++) {
                    SimpleDateFormat format = new SimpleDateFormat("M");
                    Calendar cals = Calendar.getInstance();
                    cals.add(Calendar.MONTH, -1);//查询上一个月
                    Date m = cals.getTime();
                    String mon = format.format(m);//上个月
                    int syear = cals.get(Calendar.YEAR);
                    //查询上月库存
                    wgb = new WmsGoodsBalance();
                    wgb.setId(UUIDUtils.uuid());
                    wgb.setZjrk(outStocks.get(i).getZjrk());
                    wgb.setZjck(outStocks.get(i).getZjck());
                    wgb.setHpid(outStocks.get(i).getHpid());//货品id
                    wgb.setHpbh(outStocks.get(i).getHpbh());//货品编号
                    wgb.setHpmc(outStocks.get(i).getHpmc());//货品名
                    wgb.setN(String.valueOf(year));//年
                    wgb.setY(String.valueOf(month));//月
                    wgb.setSj(sdf.format(date));
                    wgb.setShsl(outStocks.get(i).getShsl());
                    wgb.setCkid(outStocks.get(i).getCkid());
                    wgb.setHzid(outStocks.get(i).getHzid());
                    wgb.setCksl(outStocks.get(i).getCksl());
                    wgb.setHpslck(outStocks.get(i).getHpslck());
                    wgb.setHpslrk(outStocks.get(i).getHpslrk());
                    /*if (wgbs!=null){
                        wgb.setSyjc(wgbs.getByjc());//上月库存
                    }else {
                        wgb.setSyjc("0");//上月库存
                    }
                    wgb.setByjc(list.get(i).getKczsl());//本月库存*/
                    if (outStocks.get(i).getByjc() == null) {
                        int bysl = Integer.parseInt(outStocks.get(i).getShsl()) - Integer.parseInt(outStocks.get(i).getZjck()) + Integer.parseInt(outStocks.get(i).getZjrk()) - Integer.parseInt(outStocks.get(i).getCksl()) - Integer.parseInt(outStocks.get(i).getHpslck());
                        wgb.setByjc(String.valueOf(bysl));
                        WmsGoodsBalance wgb1 = dao.byjcselect(String.valueOf(syear), mon, outStocks.get(i).getHpid());
                        if (wgb1 != null) {
                            wgb.setSyjc(wgb1.getByjc());
                        } else {
                            wgb.setSyjc("0");
                        }
                    }
                    if (wgb.getHpbh() != null) {
                        lists.add(wgb);
                    }
                }
                //添加结存数据
                dao.baseInsertShelves(lists);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * 进出存合并报表查询
     */
    @BaseServiceDescribe(moduleName = "进出存合并报表", btnName = "分页查询")
    public Page<WmsGoodsBalance> selectjc(WmsGoodsBalance entity) throws Exception {
        Page<WmsGoodsBalance> list = null;
        try {
            PageHelper.startPage(entity.getPage(), entity.getRows());
            /*if (entity.getSj()!=null && !entity.equals("")){
               *//* String date =entity.getSj();
                date = date.replace("Z", " UTC");//注意是空格+UTC
                SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");//注意格式化的表达式
                Date d = format2.parse(date);*//*
                String d =entity.getSj();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                entity.setSj(sdf.format(d));
            }*/
            list= (Page<WmsGoodsBalance>) dao.selectjc(entity);
            return list;
        } catch (Exception e) {
            throw new ServiceException("进出存合并报表分页查询失败", e);
        }
    }

    /**
     * 进出存合并报表导出
     * @param idList
     * @param map
     * @return
     */
    @BaseServiceDescribe(moduleName = "进出存合并报表导出", btnName = "进出存合并报表导出")
    public List<WmsGoodsBalance> getoutStock(@Param("list") List<String> idList, @Param("Map") Map map) throws Exception {
        List<WmsGoodsBalance> list=new ArrayList<WmsGoodsBalance>();
    /*    if (map.get("sj")!=null){
            String date =map.get("sj").toString();
            date = date.replace("Z", " UTC");//注意是空格+UTC
            SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");//注意格式化的表达式
            Date d = format2.parse(date);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            map.put("sj",sdf.format(d));
        }*/
        list=dao.getoutStock(idList, map);
        return list;
    }

    /**
     * 出库日报表分页查询
     */
    @BaseServiceDescribe(moduleName = "出库日报表", btnName = "分页查询")
    public Page<Wmslibrary> baselibraryPage(Wmslibrary entity) throws Exception {
        Page<Wmslibrary> list=null;
        try {
            PageHelper.startPage(entity.getPage(), entity.getRows());
            list = (Page<Wmslibrary>) dao.baselibraryPage(entity);
            for(Wmslibrary wmslibrary:list){
                String strDate =wmslibrary.getFysj();
                String pat1 = "yyyy-MM-dd HH:mm:ss.SSS" ;
                String pat2 = "yyyy-MM-dd HH:mm:ss" ;
                SimpleDateFormat sdf1 = new SimpleDateFormat(pat1) ;
                SimpleDateFormat sdf2 = new SimpleDateFormat(pat2) ;
                Date d =sdf1.parse(strDate) ;
                wmslibrary.setFysj(sdf2.format(d));
            }
            return list;
        } catch (Exception e) {
            throw new ServiceException("出库日报表分页查询失败", e);
        }
    }
    /**
     * 出库日报表导出
     * @param entity
     * @return
     */
    @BaseServiceDescribe(moduleName = "出库日报表导出", btnName = "出库日报表导出")
    public List<Wmslibrary> getoutlibrary(@Param("Wmslibrary") Wmslibrary entity) throws ParseException {
        List<Wmslibrary> list=dao.getoutlibrary(entity);
        for(Wmslibrary wmslibrary:list){
            String strDate =wmslibrary.getFysj();
            String pat1 = "yyyy-MM-dd HH:mm:ss.SSS" ;
            String pat2 = "yyyy-MM-dd HH:mm:ss" ;
            SimpleDateFormat sdf1 = new SimpleDateFormat(pat1) ;
            SimpleDateFormat sdf2 = new SimpleDateFormat(pat2) ;
            Date d =sdf1.parse(strDate) ;
            wmslibrary.setFysj(sdf2.format(d));
        }
        return list;
    }

    /**
     * 库存报表查询
     */
    @BaseServiceDescribe(moduleName = "库存报表查询", btnName = "库存报表查询")
    public Page<WmsStockReport> getstock(@Param("WmsStockReport") WmsStockReport entity )throws Exception {
        Page<WmsStockReport> list=null;
        int zsl=0;
        int kysl=0;
        int djhsl=0;
        int dbhsl=0;
        int djsl=0;
        int dzysl=0;
        try {
            PageHelper.startPage(entity.getPage(), entity.getRows());
            list = (Page<WmsStockReport>) dao.getstock(entity);
            WmsStockReport wmsStockReport1=new WmsStockReport();
            for(WmsStockReport wmsStockReport:list){
                double sl= Double.parseDouble(wmsStockReport.getSl());
                zsl+=sl++;
                double kysls= Double.parseDouble(wmsStockReport.getKysl());
                kysl+=kysls++;
                double dxjsl= Double.parseDouble(wmsStockReport.getDxjsl());
                djhsl+=dxjsl++;
                double bhsl= Double.parseDouble(wmsStockReport.getBhsl());
                dbhsl+=bhsl++;
                double dzysls= Double.parseDouble(wmsStockReport.getDzysl());
                dzysl+=dzysls++;
                double djsls= Double.parseDouble(wmsStockReport.getDjsl());
                djsl+=djsls++;
            }
            wmsStockReport1.setHpbh("合计");
            wmsStockReport1.setSl(String.valueOf(zsl));
            wmsStockReport1.setKysl(String.valueOf(kysl));
            wmsStockReport1.setDxjsl(String.valueOf(djhsl));
            wmsStockReport1.setBhsl(String.valueOf(dbhsl));
            wmsStockReport1.setDzysl(String.valueOf(dzysl));
            wmsStockReport1.setDjsl(String.valueOf(djsl));
            list.add(wmsStockReport1);
            return list;
        }catch (Exception e){
            throw new ServiceException("库存报表分页查询", e);
        }
    }

    /**
     * 库存报表导出
     * @param idList
     * @return
     */
    @BaseServiceDescribe(moduleName = "库存报表导出", btnName = "库存报表导出")
    public List<WmsStockReport> getstockout(@Param("list") List<String> idList, @Param("Map") Map map){
        return dao.getstockout(idList, map);
    }
    /**
     * 合计统计报表
     */
    @BaseServiceDescribe(moduleName = "合计统计报表", btnName = "合计统计报表")
    public JSONObject countTjBySj(String sj, String ckid) throws Exception {
        JSONObject object = new JSONObject();
        //有库存的货品数
        Integer stockSl = wmsStockMapper.countSpslByRq(sj,ckid);
        //整箱数和散箱数
        List<WmsBox> box=wmsStockMapper.countZsslByRq(sj,ckid);
        if(box.size()>0){
        for(WmsBox b:box){
            if("0".equals(b.getZslx())){
                object.put("zxsl",b.getSl());
            }
            if("1".equals(b.getZslx())){
                object.put("sxsl",b.getSl());
            }
        }}else {
            object.put("zxsl",0);
            object.put("sxsl",0);
        }
        //发运订单数
        Integer fyddSl = wmsShipmentOrderMapper.getDdsl(sj,ckid);
        object.put("goodsSl", stockSl);
        object.put("ckdSl",fyddSl);
        //空库位数量
        Integer kkw=wmsLibraryPositionMapper.getEmptyCount(ckid);
        object.put("kkw",kkw);
        //库存数量（不包含集货库位）
        Integer kcsl=wmsStockMapper.getSum(ckid);
        object.put("kcsl",kcsl);
        //待捡货数量总和
        Integer djhsl=wmsStockMapper.getDjhSum(ckid);
        object.put("djhsl",djhsl);
        //总出库数量
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("ckid", ckid);
        map.put("sj", sj);
        Integer zcksl = wmsPickingMapper.getZckslSum(map);
        object.put("zcksl",zcksl);
        return object;
    }

    /**
     * 货品出货量
     */
    @BaseServiceDescribe(moduleName = "货品出货量", btnName = "分页查询")
    public List<WmsPicking> shipmentGoods(WmsPicking entity) throws Exception {
        try {
            List<WmsPicking> list = dao.shipmentGoods(entity);
            return list;
        } catch (Exception e) {
            throw new ServiceException("货品出货量", e);
        }
    }

    /**
     * 借货单预警
     */
    @BaseServiceDescribe(moduleName = "借货单预警", btnName = "借货单预警查询")
    public List<WmsShipmentOrder> getBorrowing(WmsShipmentOrder entity) throws Exception {
        try{
            List<WmsShipmentOrder> list=dao.getBorrowing(entity);
            return  list;
        }catch (Exception e){
            throw new ServiceException("借货单预警", e);
        }
    }
}
